<template>
  <div>
    <nav-bar>
      <template v-slot:left>&lt;</template>
      <template v-slot:default>商品详情:{{ detail.id }}</template>
    </nav-bar>
    <van-image style="margin-top: 50px" width="100%" :src="detail.cover_url" />
    <van-card
      style="text-align: left"
      :num="detail.stock"
      :price="detail.price + '.00'"
      :desc="detail.description"
      :title="detail.title"
    >
      <template #footer>
        <van-button size="mini" type="warning" @click="handleAddCart"
          >加入购物车</van-button
        >
        <van-button size="mini" type="danger" @click="goToCart"
          >立即购买</van-button
        >
      </template>
    </van-card>
    <van-tabs v-model="active">
      <van-tab title="概述">
        <div class="content" v-html="detail.details"></div>
      </van-tab>
      <van-tab title="热评">
        <div v-for="item in detail.comments" :key="item">
          {{ item }}
        </div>
      </van-tab>
      <van-tab title="相关图书">
        <goods-list :goods="like_goods"></goods-list>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import { useRoute, useRouter } from "vue-router";
import { ref, onMounted, reactive, toRefs } from "vue";
import { getDetail } from "network/detail.js";
import { addCart } from "network/cart.js";
import GoodsList from "components/content/goods/GoodsList";
import { useStore } from "vuex";
import { Toast } from "vant";
export default {
  name: "Detail",
  components: {
    NavBar,
    GoodsList,
  },
  setup() {
    const store = useStore();
    const active = ref(2);
    const route = useRoute();
    const router = useRouter();
    let id = ref(0);
    let book = reactive({
      detail: {},
      like_goods: [],
    });

    onMounted(() => {
      Toast.loading({ message: "加载中。。。", forbidClick: true });
      id.value = route.query.id;
      getDetail(id.value).then((res) => {
        book.detail = res.goods;
        book.like_goods = res.like_goods;
      });
      Toast.clear();
    });

    // 添加购物车
    const handleAddCart = () => {
      addCart({
        goods_id: book.detail.id,
        num: 1,
      }).then((res) => {
        if (res.status && (res.status == "204" || res.status == "201")) {
          // alert("添加成功");

          // 设置store中cartCount购物车的数量
          store.dispatch("updataCart");
        }
      });
    };

    // 立即购买
    const goToCart = () => {
      addCart({
        goods_id: book.detail.id,
        num: 1,
      }).then((res) => {
        if (res.status && (res.status == "204" || res.status == "201")) {
          // 设置store中cartCount购物车的数量
          store.dispatch("updataCart");
          router.push({
            path: "/shopcart",
          });
        }
      });
    };
    return {
      id,
      ...toRefs(book),
      active,
      handleAddCart,
      goToCart,
    };
  },
};
</script>

<style scoped lang="scss">
.content {
  padding: 10px;
  img {
    max-width: 100%;
    height: auto;
  }
}
</style>
